<template>
    <div :class="`bg-${navbarColor}`">
        <nav class="border-b border-gray-100" :class="`bg-${navbarColor}`" style="position: fixed; z-index: 100; width: 100%">
            <div class="mx-auto flex h-16 max-w-7xl items-center justify-between px-8">
                <!-- 左侧导航区域 -->
                <div class="flex items-center space-x-10">
                    <!-- Logo区域 -->
                    <div class="flex items-center gap-4">
                        <img src="/src/assets/img/LOGO.png" alt="logo" 
                             class="w-11 h-11 object-cover transition-transform hover:scale-105 duration-300"/>
                        <span class="text-lg font-medium text-gray-800">人才画像系统</span>
                    </div>

                    <!-- 导航菜单项（统一图标尺寸） -->
                    <div class="flex items-center space-x-10">
                        <a v-for="(item, index) in navItems" :key="index" 
                           :class="[
                               'text-sm font-medium px-5 py-3 rounded-md transition-all',
                               route.path.indexOf(item.path) === 0 
                                 ? 'text-blue-600 bg-blue-50 shadow-sm' 
                                 : 'text-gray-600 hover:bg-blue-50 hover:text-blue-600'
                           ]" 
                           @click="jump(item.path)">
                            <!-- 统一导航图标为18px -->
                            <i :class="item.icon" class="mr-3 text-[18px] align-middle"></i>
                            {{ item.name }}
                        </a>
                    </div>
                </div>

                <!-- 右侧功能区（优化间距） -->
                <div class="flex items-center space-x-5">
                    <div class="flex items-center space-x-4">
                        <!-- 机器人图标调整 -->
                        <button class="p-3 text-gray-600 hover:bg-gray-100 rounded-full transition-colors duration-300"
                                @click="showAIChat = true">
                            <i class="iconfont icon-jiqiren_o text-[24px]"></i> <!-- 24px -->
                        </button>
                        <!-- 消息图标调整 -->
                        <button class="p-3 text-gray-600 hover:bg-gray-100 rounded-full transition-colors duration-300"
                                @click="centerDialogVisible = true">
                            <el-icon class="text-[24px]"><Message /></el-icon> <!-- 24px -->
                        </button>
                    </div>

                    <!-- 用户信息区域 -->
                    <el-dropdown trigger="click">
                        <div class="flex cursor-pointer items-center space-x-3 group">
                            <div class="relative">
                                <div class="h-9 w-9 overflow-hidden rounded-full border-2 border-transparent group-hover:border-blue-100 transition-all duration-300">
                                    <img :src="avatarUrl" alt="用户头像" 
                                         class="h-full w-full object-cover" 
                                         @click="jump('/personalcenter')"/>
                                </div>
                                <div v-if="isOnline"
                                    class="absolute bottom-0 right-0 h-2.5 w-2.5 rounded-full border-2 border-white bg-green-500">
                                </div>
                            </div>
                            <div class="flex items-center space-x-3"> <!-- 文字间距扩大 -->
                                <span class="text-sm font-medium text-gray-700">{{ userName }}</span>
                                <el-icon class="text-gray-500 transition-colors duration-300 group-hover:text-blue-500">
                                    <CaretBottom />
                                </el-icon>
                            </div>
                        </div>
                        
                        <!-- 下拉菜单 -->
                        <template #dropdown>
                            <el-dropdown-menu class="!py-2 !rounded-xl">
                                <el-dropdown-item 
                                    v-for="(item, index) in dropdownItems" 
                                    :key="index"
                                    class="px-4 py-2.5 hover:bg-blue-50 transition-colors duration-300">
                                    <div class="flex items-center space-x-3">
                                        <el-icon class="text-lg">
                                            <component :is="item.icon" />
                                        </el-icon>
                                        <span class="text-sm" @click="jump(item.path)">{{ item.label }}</span>
                                    </div>
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>
        </nav>

        <!-- 弹窗组件 -->
        <el-dialog v-model="centerDialogVisible" width="80%" align-center>
            <email></email>
        </el-dialog>
        <el-dialog v-model="showAIChat" width="60%" class="ai-chat-dialog" 
                 :modal="true" :close-on-click-modal="false">
            <div class="relative">
                <button class="absolute top-3 right-3 text-gray-600 hover:text-gray-800 transition-colors duration-300"
                        @click="showAIChat = false">
                    <i class="fas fa-times text-xl"></i>
                </button>
                <h3 class="text-xl font-semibold mb-4">AI问答</h3>
                <div class="ai-chat-content">
                    <aiChat></aiChat>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Message, CaretBottom } from '@element-plus/icons-vue';
import email from "../components/student/emailbox.vue";
import aiChat from "../components/student/aiChat.vue";
import { getStudentBaseInfo } from "../api/student/personCenter/stuPersonCenter";

const router = useRouter();
const route = useRoute();

// 路由颜色配置
const routeColors = {
    '/report': 'blue',
    '/certificateAuthority': 'blue',
    '/assessment': 'blue',
    '/resume': 'blue',
    '/course': 'blue',
    '/position': 'blue'
};

// 响应式数据
const navbarColor = ref('white');
const avatarUrl = ref('');
const isOnline = ref(true);
const userName = ref('');
const centerDialogVisible = ref(false);
const showAIChat = ref(false);

// 导航配置
const navItems = [
    { name: '素质报告', path: '/report', icon: 'iconfont icon-baogao' },
    { name: '认证中心', path: '/certificateAuthority', icon: 'iconfont icon-renzheng' },
    { name: '测评中心', path: '/assessment', icon: 'iconfont icon-zaixianceping' },
    { name: '简历中心', path: '/resume', icon: 'iconfont icon-resume-line' },
    { name: '课程中心', path: '/course', icon: 'iconfont icon-resume-line' },
    { name: '招聘中心', path: '/position', icon: 'iconfont icon-gangweiguanli' }
];

const dropdownItems = [
    { label: '个人中心', icon: 'User', path: '/personalcenter' },
    { label: '修改密码', icon: 'Edit', path: '/updatePassword' },
    { label: '退出登录', icon: 'SwitchButton', path: '/' },
];

// 生命周期
onMounted(async () => {
    try {
        const userInfo = JSON.parse(localStorage.getItem("userInfo") || '{}');
        userName.value = userInfo.userName || '用户';
        const data = await getStudentBaseInfo();
        avatarUrl.value = data.userImg || '/default-avatar.jpg';
    } catch (error) {
        console.error('初始化错误:', error);
    }
});

// 路由监听
watch(() => route.path, (newPath) => {
    navbarColor.value = routeColors[newPath as keyof typeof routeColors] || 'white';
});

// 路由跳转方法
function jump(path: string) {
    if (path === '/') {
        localStorage.removeItem("prehaslogin");
        localStorage.removeItem("userInfo");
    }
    router.push(path);
}
</script>

<style scoped>
/* 导航项动画优化 */
.nav-link {
    transition: 
        transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
        background-color 0.2s ease;
}

.nav-link:hover {
    transform: translateY(-1px);
}

/* 工具按钮悬停效果增强 */
.tool-button {
    transition: 
        background-color 0.2s ease,
        transform 0.2s ease,
        box-shadow 0.2s ease;
}

.tool-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

/* 用户头像悬停效果 */
.user-avatar {
    transition: 
        border-color 0.3s ease,
        transform 0.3s ease;
}

.user-avatar:hover {
    transform: scale(1.05);
}
/* 图标尺寸系统 */
/* 导航图标 */
.nav-items .iconfont {
    font-size: 1.125rem; /* 18px */
    vertical-align: -1px;
}

/* 工具图标 */
.tool-button .iconfont,
.tool-button .el-icon {
    font-size: 1.5rem; /* 24px */
}

/* 响应式调整 */
@media (max-width: 768px) {
    .nav-items .iconfont {
        font-size: 1rem !important;  /* 16px */
    }
    .tool-button .iconfont,
    .tool-button .el-icon {
        font-size: 1.25rem !important;  /* 20px */
    }
}

/* 响应式调整优化 */
@media (max-width: 1440px) {
    .nav-items {
        space-x: 5;
        padding: 0 0.5rem;
    }
}

@media (max-width: 1280px) {
    .nav-items {
        display: none;
    }
    .logo-text {
        display: none;
    }
    .tool-buttons {
        space-x: 2;
    }
}

.ai-chat-dialog .el-dialog__header {
    display: none;
}

/* 激活状态指示器优化 */
.router-link-active::after {
    content: '';
    @apply absolute bottom-0 left-1/2 w-6 h-[2px] bg-blue-500 rounded-full;
    transform: translateX(-50%);
    transition: width 0.3s ease;
}
</style>